package bigdecimal;
import java.math.BigDecimal;
import java.math.RoundingMode;

/*
    四则运算的方法:
        加 : BigDecimal add(BigDecimal augend)
        减 : BigDecimal subtract(BigDecimal subtrahend)
        乘 : BigDecimal multiply(BigDecimal multiplicand)
        除 : BigDecimal divide(BigDecimal divisor)
        带有保留小数点后几位和舍入模式的除法:
            BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
                     int scale: 保留小数点后几位
                     RoundingMode : 舍入模式 RoundingMode: 枚举类 (类名.枚举项)
                        向上取整 : RoundingMode.UP/RoundingMode.CEILING (正数情况是一样的)
                        向下取整 : RoundingMode.DOWN/RoundingMode.FLOOR(正数情况是一样的)
                        四舍五入 : RoundingMode.HALF_UP

 */
public class BigDecimalDemo2 {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("0.1");
        BigDecimal bd2 = new BigDecimal("0.8");

        System.out.println("bd1.add(bd2) = " + bd1.add(bd2));
        System.out.println("bd1.subtract(bd2) = " + bd1.subtract(bd2));
        System.out.println("bd1.multiply(bd2) = " + bd1.multiply(bd2));
        System.out.println("bd1.divide(bd2) = " + bd1.divide(bd2));//没有处理保留位数和四舍五入


        System.out.println("bd1.divide(bd2,2, RoundingMode.UP) = " + bd1.divide(bd2, 2, RoundingMode.UP));//0.13
        System.out.println("bd1.divide(bd2,2, RoundingMode.DOWN) = " + bd1.divide(bd2, 2, RoundingMode.DOWN));//0.12
        System.out.println("bd1.divide(bd2,2, RoundingMode.HALF_UP) = " + bd1.divide(bd2, 2, RoundingMode.HALF_UP));//0.13

    }
}
